/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.incad.psh.pshassign.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.Widget;
import cz.incad.psh.pshassign.client.rpc.PSHService;
import cz.incad.psh.pshassign.client.rpc.PSHServiceAsync;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author alberto
 */
public class StopWords extends Composite {

    private static StopWordsUiBinder uiBinder = GWT.create(StopWordsUiBinder.class);
    ArrayList<String> stopWords;
    PSHServiceAsync pshService = GWT.create(PSHService.class);
    @UiField
    ListBox stopList;
    @UiField
    PushButton addBt;
    @UiField
    PushButton removeBt;

    interface StopWordsUiBinder extends UiBinder<Widget, StopWords> {
    }

    public StopWords() {
        initWidget(uiBinder.createAndBindUi(this));
        addBt.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                final String word = Window.prompt("Word to add", "").trim();
                if(word.length()>0 && !stopWords.contains(word)){
                    
                    try {
                        pshService.addStopWord(word, new AsyncCallback() {
                            @Override
                            public void onFailure(Throwable caught) {
                                Window.alert(caught.getLocalizedMessage());
                            }

                            @Override
                            public void onSuccess(Object result) {
                                stopWords.add(word);
                                stopList.addItem(word, word);
                                Window.alert("word added!");
                                
                            }
                        });
                    } catch (Exception ex) {
                        Logger.getLogger(StopWords.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });

        removeBt.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                final String word = stopList.getValue(stopList.getSelectedIndex());
                try {
                    pshService.removeStopWord(word, new AsyncCallback() {
                        @Override
                        public void onFailure(Throwable caught) {
                            Window.alert(caught.getLocalizedMessage());
                        }

                        @Override
                        public void onSuccess(Object result) {
                            stopWords.remove(word);
                            stopList.removeItem(stopList.getSelectedIndex());
                            Window.alert("word removed!");
                        }
                    });
                } catch (Exception ex) {
                    Logger.getLogger(StopWords.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    @Override
    protected void onLoad() {
        try {
            pshService.getStopWords(new AsyncCallback() {
                @Override
                public void onFailure(Throwable caught) {
                    Window.alert(caught.getLocalizedMessage());
                }

                @Override
                public void onSuccess(Object result) {
                    stopWords = new ArrayList<String>((ArrayList<String>) result);
                    for (String s : stopWords) {
                        stopList.addItem(s, s);
                    }
                }
            });
        } catch (Exception ex) {
            Logger.getLogger(Options.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
